﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MvcAtletIS.Models;

namespace MvcAtletIS.Areas.MyClub.Controllers
{
    [Authorize(Roles = "Users")]
    public class SignupsController : Controller
    {
        atletisEntities db = new atletisEntities();

        // GET: /MyClub/Signups/
        public ActionResult Index(int CompetitionId = 0, int DisciplineListId = 0, string status = "")
        {
            string id = User.Identity.Name;

            var competitions = db.Competitions.ToList();
            var disciplineList = db.DisciplineList.Where(dl => dl.CompetitionId == CompetitionId).ToList();
            var competitors = db.Competitors.Where(c => c.ClubId == id & c.Active == true & c.Competitor == true).OrderBy(c => c.Surname).ToList();

            ViewBag.Clubs = db.Clubs.Single(c => c.ID == id);
            ViewBag.CompetitionId = new SelectList(competitions, "ID", "Name", CompetitionId);
            ViewBag.DisciplineListId = new SelectList(disciplineList, "ID", "Name", DisciplineListId);
            ViewBag.CompetitorId = new SelectList(competitors, "ID", "FullName");
            ViewBag.Status = status;

            return View();
        }

        [HttpPost]
        public ActionResult Index(string id, Performances model, int CompetitionId = 0, int DisciplineListId = 0)
        {
            // Competitor data, SB
            if (model.CompetitorId != null)
            {
                var competitor = db.Competitors.SingleOrDefault(c => c.ID == model.CompetitorId);
                model.FirstName = competitor.Firstname;
                model.Surname = competitor.Surname;
                model.Birthdate = competitor.Birthdate;
                model.Club = competitor.Clubs.Name;
                model.CountryId = competitor.CountryId;

                var modelDisciplineList = db.DisciplineList.Single(dl => dl.ID == model.DisciplineListId);
                model.SB = 0;   // nacitat SB zo statistiky
            }

            model.StartNumber = 0;
            model.Lane = 0;
            model.Code = 0;
            model.Result = 0;
            model.AdvResult = 0;
            model.SecondaryRank = 0;
            model.Reaction = string.Empty;
            model.SMS = string.Empty;
            model.Points = 0;
            model.Sync = true;
            model.Rank = 0;

            ModelState.Clear();
            TryValidateModel(model);

            if (ModelState.IsValid)
            {
                db.Performances.AddObject(model);
                db.SaveChanges();

                model = new Performances();

                return RedirectToAction("Index", new { CompetitionId = CompetitionId, DisciplineListId = DisciplineListId, status = "Vaše prihlášenie bolo úspešné!" });
            }

            var competitions = db.Competitions.ToList();
            var disciplineList = db.DisciplineList.Where(dl => dl.CompetitionId == CompetitionId).ToList();
            var competitors = db.Competitors.Where(c => c.ClubId == id & c.Active == true & c.Competitor == true).OrderBy(c => c.Surname).ToList();

            ViewBag.Clubs = db.Clubs.Single(c => c.ID == id);
            ViewBag.CompetitionId = new SelectList(competitions, "ID", "Name", CompetitionId);
            ViewBag.DisciplineListId = new SelectList(disciplineList, "ID", "Name", DisciplineListId);
            ViewBag.CompetitorId = new SelectList(competitors, "ID", "FullName");
            ViewBag.Status = "Prihlásenie nebolo úspešné.";

            return View(model);
        }

        #region CascadeDropDown

        private IList<DisciplineList> GetDisciplineList(int id)
        {
            return db.DisciplineList.Where(dl => dl.CompetitionId == id & dl.ParentId == 0).OrderBy(dl => dl.ExternalId).ToList();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult LoadDisciplinesForCompetition(string id)
        {
            var disciplineList = this.GetDisciplineList(Convert.ToInt32(id));

            var disciplineData = disciplineList.Select(m => new SelectListItem()
            {
                Text = m.Name,
                Value = m.ID.ToString(),

            });

            return Json(disciplineData, JsonRequestBehavior.AllowGet);
        }

        #endregion

    }
}
